<?php
declare(strict_types=1);

namespace Service\LayoutService;
use Service\ActivityService\ActivityStoreService;
use Service\BaseService;
use Service\DecorateService\DecorateService;
use Service\GoodsService\GoodsLinkService;
use App\Model\BbcModel\GoodsClass;
use App\Model\BbcModel\Layout;
use App\Model\BbcModel\GoodsBrand;
use App\Model\BbcModel\Specialtopic;
use Service\ActivityService\ActivityService;

/**
 * 图片广告组件
 * Created by PhpStorm
 * User: Administrator
 * Date: 2023/5/16
 * Time: 09:52:46
 */
class newImageAd extends BaseService
{
    /**
     * @param $mnid
     * @param $cid
     * @param $mn_sid
     * @param $mid
     * @param $moduleData
     * @return array
     */
    public function getDataList($mnid,$cid,$mn_user,$mid,$moduleData,$extra){
        $mn_sid    = $mn_user['id'];
        $sub_entry = $moduleData['setStyle']['sub_entry'] ?? [];
        if(count($sub_entry) <= 0){
            return [];
        }

        $storeActivity = $extra['storeActivity'] ?? [];
        //获取店铺活动数据
        $activityID = $storeActivity['seckillActivity']['id'] ?? 0;
        $mn_secskill_activity = ActivityStoreService::instance()->getMnActivityOne((int)$cid,(int)$mn_sid,$activityID);
        $sub_entry_group = getArrayGroupData($sub_entry,'link_type');

        //跳转分类
        $class_ids_map = [];
        if(isset($sub_entry_group[DecorateService::instance()->link_type['classify']])){
            $link_ids = array_column($sub_entry_group[DecorateService::instance()->link_type['classify']],'link_id');
            $class_ids = GoodsClass::instance()
                ->where([
                    ['cid','=',$cid],
                    ['df','=',0]
                ])
                ->whereIn('id',$link_ids)
                ->select(['id','name'])
                ->get()->toArray();
            $class_ids_map = array_column($class_ids,null,'id');
        }

        //跳转微页面
        $page_ids = [];
        if(isset($sub_entry_group[DecorateService::instance()->link_type['microPage']])){
            $link_ids = array_column($sub_entry_group[DecorateService::instance()->link_type['microPage']],'link_id');
            $page_list = Layout::instance()
                ->where([
                    ['cid','=',$cid],
                    ['df','=',0],
                    ['status','=',1]
                ])
                ->whereIn('id',$link_ids)
                ->get()->toArray();
            $page_ids = array_column($page_list,'id');
        }

        //跳品牌
        $brand_ids = [];
        if(isset($sub_entry_group[DecorateService::instance()->link_type['brand']])){
            $link_ids = array_column($sub_entry_group[DecorateService::instance()->link_type['brand']],'link_id');
            $brand_list = Goodsbrand::instance()
                ->where([
                    ['cid','=',$cid],
                    ['df','=',0],
                ])
                ->whereIn('id',$link_ids)
                ->get()->toArray();
            $brand_ids = array_column($brand_list,'id');
        }

        //跳转普通商品
        $goods_ids = [];
        if(isset($sub_entry_group[DecorateService::instance()->link_type['ordinaryGoods']])){
            $link_ids = array_column($sub_entry_group[DecorateService::instance()->link_type['ordinaryGoods']],'link_id');
            $goods_list = GoodsLinkService::instance()->getGoodsMnLinkList($mn_user,$link_ids);
            $goods_ids = array_column((array)$goods_list,'id');
        }

        //跳转专题馆
        $specialtopic_ids = [];
        if(isset($sub_entry_group[DecorateService::instance()->link_type['subjectList']])){
            $link_ids = array_column($sub_entry_group[DecorateService::instance()->link_type['subjectList']],'link_id');
            $specialtopic_list = Specialtopic::instance()
                ->where([
                    ['cid','=',$cid],
                    ['df','=',0],
                    ['status','=',1]
                ])
                ->whereIn('id',$link_ids)
                ->select(['id','name'])
                ->get()
                ->toArray();
            $specialtopic_ids = array_column($specialtopic_list,'id');
        }

        //跳转秒杀，拼团，满减
        $activity_ids_map = [];
        if(
            isset($sub_entry_group[DecorateService::instance()->link_type['seckillList']]) ||
            isset($sub_entry_group[DecorateService::instance()->link_type['groupBookingList']]) ||
            isset($sub_entry_group[DecorateService::instance()->link_type['moneyOff']])
        ){
            $a_link_ids = [];
            $b_link_ids = [];
            $c_link_ids = [];
            if(isset($sub_entry_group[DecorateService::instance()->link_type['seckillList']])){
                $a_link_ids = array_column($sub_entry_group[DecorateService::instance()->link_type['seckillList']],'link_id');
            }

            if(isset($sub_entry_group[DecorateService::instance()->link_type['groupBookingList']])){
                $b_link_ids = array_column($sub_entry_group[DecorateService::instance()->link_type['groupBookingList']],'link_id');
            }

            if(isset($sub_entry_group[DecorateService::instance()->link_type['moneyOff']])){
                $c_link_ids = array_column($sub_entry_group[DecorateService::instance()->link_type['moneyOff']],'link_id');
            }

            $link_ids = array_merge($a_link_ids,$b_link_ids,$c_link_ids);
            if(count($link_ids) > 0){
                $returnActivity = ActivityService::instance()->getActivityIdsLink((int)$cid,(int)$mn_sid,$mn_user,$link_ids,[]);
                $activity_ids_map = array_column($returnActivity,null,'activity_id');
            }
        }

        //跳转秒杀商品，拼团商品
        $returnActivityGoods = [];
        if(
            isset($sub_entry_group[DecorateService::instance()->link_type['seckillGoods']]) ||
            isset($sub_entry_group[DecorateService::instance()->link_type['groupBookingGoods']])
        ){
            $a_link_ids = [];
            $a_activity_ids = [];
            if(isset($sub_entry_group[DecorateService::instance()->link_type['seckillGoods']])){
                $a_link_ids = array_column($sub_entry_group[DecorateService::instance()->link_type['seckillGoods']],'link_id');
                $a_activity_ids = array_column($sub_entry_group[DecorateService::instance()->link_type['seckillGoods']],'activity_id');
            }

            $b_link_ids = [];
            $b_activity_ids = [];
            if(isset($sub_entry_group[DecorateService::instance()->link_type['groupBookingGoods']])){
                $b_link_ids = array_column($sub_entry_group[DecorateService::instance()->link_type['groupBookingGoods']],'link_id');

                $b_activity_ids = array_column($sub_entry_group[DecorateService::instance()->link_type['groupBookingGoods']],'activity_id');
            }

            $link_ids = array_merge($a_link_ids,$b_link_ids);
            $activity_ids = array_merge($a_activity_ids,$b_activity_ids);
            $returnActivityGoods = ActivityService::instance()->getActivityIdsLink((int)$cid,(int)$mn_sid,$mn_user,$activity_ids,$link_ids);
        }
        foreach ($sub_entry as &$v){

            if(!$v['link_id']){ //跳转没有值
                continue;
            }
            $v['target_type'] = 0;

            //跳转分类
            if($v['link_type'] == DecorateService::instance()->link_type['classify']){
                if(isset($class_ids_map[$v['link_id']])){ //商品分类状态正常
//                    $v['title'] = $class_ids_map[$v['link_id']]['name'] ?? '';
                }else{
                    $v['link_id'] = '';
                }
            }

            //跳转微页面
            if($v['link_type'] == DecorateService::instance()->link_type['microPage']){
                if(!in_array($v['link_id'],$page_ids)){ //微页面状态不正常
                    $v['link_id'] = '';
                }
            }

            //跳转品牌
            if($v['link_type'] == DecorateService::instance()->link_type['brand']){
                if(!in_array($v['link_id'],$brand_ids)){ //商品不正常
                    $v['link_id'] = '';
                }
            }

            //跳转普通商品
            if($v['link_type'] == DecorateService::instance()->link_type['ordinaryGoods']){
                if(!in_array($v['link_id'],$goods_ids)){ //商品不正常
                    $v['link_id'] = '';
                }
            }

            //跳转专题馆
            if($v['link_type'] == DecorateService::instance()->link_type['subjectList']){
                if(!in_array($v['link_id'],$specialtopic_ids)){ //专题馆不正常
                    $v['link_id'] = '';
                }
            }

            //跳转秒杀
            if($v['link_type'] == DecorateService::instance()->link_type['seckillList']){
                if(isset($activity_ids_map[$v['link_id']])){ //店铺存在此活动
                    if($activity_ids_map[$v['link_id']]['object_status'] != 1){ //活动不正常
                        $v['link_id'] = '';
                    }
                    $v['target_type'] = $activity_ids_map[$v['link_id']]['target_type'] ?? 1;
                }else{
                    $v['link_id'] = '';
                    $v['target_type'] = 0;
                }
            }

            //跳转拼团
            if($v['link_type'] == DecorateService::instance()->link_type['groupBookingList']){
                if(isset($activity_ids_map[$v['link_id']])){ //店铺存在此活动
                    if($activity_ids_map[$v['link_id']]['object_status'] != 1){ //活动不正常
                        $v['link_id'] = '';
                    }
                    $v['target_type'] = $activity_ids_map[$v['link_id']]['target_type'] ?? 1;
                }else{
                    $v['link_id'] = '';
                    $v['target_type'] = 0;
                }
            }

            //跳转满减
            if($v['link_type'] == DecorateService::instance()->link_type['moneyOff']){
                if(isset($activity_ids_map[$v['link_id']])){ //店铺存在此活动
                    if($activity_ids_map[$v['link_id']]['object_status'] != 1){ //活动不正常
                        $v['link_id'] = '';
                    }
                    $v['target_type'] = $activity_ids_map[$v['link_id']]['target_type'] ?? 1;
                }else{
                    $v['link_id'] = '';
                    $v['target_type'] = 0;
                }
            }

            //跳转秒杀商品
            if($v['link_type'] == DecorateService::instance()->link_type['seckillGoods']){
                if(count($returnActivityGoods) <= 0){
                    $v['link_id'] = '';
                    continue;
                }
                $result = $this->activity_goods($returnActivityGoods,$v['activity_id'],(int)$v['link_id']);
                if($result['object_status'] != 1){ //活动商品不正常
                    $v['link_id'] = '';
                }
                $v['target_type'] = $result['target_type'] ?? 0;
            }

            //跳转拼团商品
            if($v['link_type'] == DecorateService::instance()->link_type['groupBookingGoods']){
                if(count($returnActivityGoods) <= 0){
                    $v['link_id'] = '';
                    continue;
                }
                $result = $this->activity_goods($returnActivityGoods,$v['activity_id'],(int)$v['link_id']);
                if($result['object_status'] != 1){ //活动商品不正常
                    $v['link_id'] = '';
                }
                $v['target_type'] = $result['target_type'] ?? 0;
            }

            //target_type 0-非会员活动，1-会员活动
            if($v['target_type'] == 1){
                $v['target_type'] = 0;
            }else if($v['target_type'] != 0){
                $v['target_type'] = 1;
            }
        }
        unset($moduleData['setStyle']['sub_entry']);
        $moduleData['setStyle']['sub_entry'] = $sub_entry;
        return $moduleData;
    }

    /**
     * 活动商品状态判断
     * @param array $returnActivity
     * @return array
     */
    public function activity_goods( $returnActivity, $activity_id, $goods_id){
        $activity_arr = getArrayGroupData($returnActivity,'activity_id');
        if(!isset($activity_arr[$activity_id])){
            $returnData['target_type']   = 0;
            $returnData['object_status'] = 0;
        }else{
            $goods_ids = array_column($activity_arr[$activity_id],'goods_id');
            if(!in_array($goods_id,$goods_ids)){
                $returnData['target_type']   = 0;
                $returnData['object_status'] = 0;
            }else{
                $returnActivity      = $activity_arr[$activity_id];
                $current_goods = filterByValue($returnActivity,'goods_id',(string)$goods_id);
                $returnData['object_status'] = $current_goods['object_status'];
                if ( $returnData['object_status'] == 0 ) {
                    $returnData['object_id'] = 0;
                }
                $returnData['target_type']   = $current_goods['target_type'];
            }
        }
        return $returnData;
    }
}